# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
import pymongo
from itemadapter import ItemAdapter


class MyprojectPipeline:

    collection_name = 'scrapy_Detail'

    def __init__(self,mongo_url,mongo_db):
        self.mongo_url = mongo_url
        self.mongo_db = mongo_db

    @classmethod
    def from_crawler(cls,crawler):
        return cls(
            mongo_url = crawler.settings.get("MONGODB_URI"),
            mongo_db = crawler.settings.get("MONGODB_DATABASE","items")
        )

    def open_spider(self,spider):
        self.client = pymongo.MongoClient(self.mongo_url)
        self.db = self.client[self.mongo_db]
    

    def close_spider(self,spider):
        self.client.close()

    def process_item(self, item, spider):
        if spider.name == 'List':
            collection_name = 'scrapy_list'
            print("------------------------------->>>",len(item))
            self.db[self.collection_name].insert_one(ItemAdapter(item))

        elif spider.name == 'Detail':
            collection_name = 'scrapy_DetailTest'
            print("------------------------------->>>",len(item))
            self.db[self.collection_name].insert_one(ItemAdapter(item))
        else :
            collection_name = self.collection_name
            print("-------------------->",collection_name)
        return item
